package com.amkai.aes.webservice.binary.objecthandler.casehistory;

import java.io.Serializable;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;

import com.amkai.aes.aescommon.service.binary.job.ChartDocAppAssocJob;
import com.amkai.aes.aescommon.service.result.casehistory.ChartDocAppTypeResult;
import com.amkai.aes.generatedbeans.cdmcasehistory.items.ChartDocumentAppointmentTypeAssociation;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class ChartDocAppAssocHandler implements JobObjectHandler<ChartDocAppAssocJob, Serializable, ChartDocAppTypeResult> {
	private static final Log logger = LogFactory.getLog(ChartDocAppAssocHandler.class);

	private final String ALL_QUERY = "select assoc FROM ChartDocumentAppointmentTypeAssociation assoc " + " LEFT JOIN FETCH assoc.appointmentType " + " LEFT JOIN FETCH assoc.chartDocument where assoc.chartDocument = :chartDoc ";

	@Override
	@SuppressWarnings("unchecked")
	public ChartDocAppTypeResult process(final ChartDocAppAssocJob job, final Serializable object) throws Exception {
		ChartDocAppTypeResult result = new ChartDocAppTypeResult();
		SessionManager sm = SiteManager.Start();
		ArrayList<ChartDocumentAppointmentTypeAssociation> resultList = new ArrayList<ChartDocumentAppointmentTypeAssociation>();
		sm.openSession();
		try {
			Query q = sm.execute(ALL_QUERY);
			q.setParameter("chartDoc", job.getChartDoc());

			resultList.addAll(q.list());
		} catch (Exception e) {
			ChartDocAppAssocHandler.logger.error("PreferenceCardQueryHandler: ", e);
			throw e;
		} finally {
			sm.closeSession();
		}

		result.setAssoc(resultList);
		return result;
	}

}
